package sprites;

import java.awt.image.BufferedImage;

import com.golden.gamedev.GameObject;
import com.golden.gamedev.object.AnimatedSprite;

/**
 * The generic sprite to be used in Bubblefish Bob. All other sprites are
 * descended from this class.
 */
public abstract class BubblefishBobSprite extends AnimatedSprite
{

    protected GameObject myOwner;

    /*
     * These are variables we need for bobbing up and down.
     * 
     * realy: the "real" y position that is maintained during bobs
     * bobCount: a dequantized counter that increments along with the game's
     *           elapsed time
     * bobFactor: how much the sprite should bob (subclasses can change this)
     */
    protected double realy;
    protected long bobCount;
    protected double bobFactor;

    /**
     * Creates a new sprite in the given coordinates.
     * @param owner the game object that controls this sprite
     * @param image the animation image
     * @param x x-coordinate
     * @param y y-coordinate
     */
    public BubblefishBobSprite(GameObject owner, BufferedImage[] image,
            double x, double y)
    {
        super(image, x, y);
        myOwner = owner;
        realy = y;
        bobFactor = 5.0;
    }

    @Override
    public void update(long elapsedTime)
    {
        super.update(elapsedTime);

        /*
         * Make stuff bob up and down.
         */
        bobCount += elapsedTime / 10;
        bob();
    }

    /**
     * Makes sprites bob up and down in the water.
     * @param elapsedTime
     */
    protected void bob()
    {
        /*
         * The second part of the sine function (the one with getX()) is there
         * to stagger the bobbing of sprites according to their x coordinate,
         * so that we get a nice wavy effect, instead of sprites bobbing up and
         * down all at the same time.
         * 
         * 480.0 represents the width of the screen. Perhaps we can put this
         * in a constant or something later.
         */
        double b = bobFactor;
        double dy = b*Math.sin((bobCount/ 30.0) + 5.0*(getX()/480.0));
        setY(realy + dy);
    }

}
